<?php 
if(KT!='KhiThe') exit;
class news_controller extends news_model{
		
	public function __construct(){
		global $lang;
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
		parent::addObject('khithe_paging');
		parent::addObject('khithe_upload');
	}
	
	public function index_action()
	{
		$this->lang['duyet'] ='';
		if($_SESSION['Mod']=='Admin'){
			
			$this->lang['duyet'] = '
			<input {dis} class="clBut" type="button" value=" {open} "  onclick="processChknew(\'open\')" />
            <input {dis} class="clBut" type="button" value=" {close} " onclick="processChknew(\'close\')" />
			<input {dis} class="clBut" type="button" value=" {del} " onclick="processChknew(\'del\')" />
			<input {dis} class="clBut" type="button" value=" {yesOK} " onclick="processChknew(\'yesok\')" /> 
            <input {dis} class="clBut" type="button" value=" {noOK} " onclick="processChknew(\'nook\')" />';
		}
		
		$limit=20;
		$parentID = ($this->valUrl(1)==''?'all':$this->valUrl(1));
		$totalRows = mysql_num_rows(parent::getSqlnews($parentID,'',''));
		$strPaging = $this->display_paging($totalRows, intval($this->valUrl(2)), intval($limit), url_base.'admin/news/index/'.$parentID.'', $this->lang,'/');
		$this->lang['paging'] = $strPaging;
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$this->lang['ComboxStatus']=$this->getCategory(0,0,0,$parentID,1);
		$resultHtml =$this->getnew($parentID, intval($limit));
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlnew'=>$resultHtml));
		$this->lang['dis'] ='';
		$this->lang['sms_err']='';
		if(empty($totalRows)){
			$this->lang['dis'] = 'disabled="disabled"';
			$this->lang['sms_err']= '<h5 style="color:#F00">'.$this->lang['errnot'].'</h5>';
		}
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
				
		return $strHtmlContent;
	}
	
	public function add_action()
	{	
		if($_GET['iframe']=='true')
		{
			if(isset($_POST['txt_titlenew']) && $_POST['txt_titlenew']!=""){

                if(empty($_POST['txt_seo_key'])){
                    $_POST['txt_seo_key'] = $_POST['txt_titlenew'];
                }

                if(empty($_POST['txt_seo_h1'])){
                    $_POST['txt_seo_h1'] = $_POST['txt_titlenew'];
                }

                if(empty($_POST['txt_title_page'])){
                    $_POST['txt_title_page'] = $_POST['txt_titlenew'];
                }

                if(empty($_POST['txt_description'])){
                    $_POST['txt_description'] = $_POST['txt_titlenew'];
                }
								
				if(parent::mdcheckSeoKey($_POST['txt_seo_key'])>0)
				{
					echo '<script type="text/javascript">alert("'.$this->lang['sms_same_key'].'");</script>';
				}
				else{
					$finish = parent::updateInsertnew($_POST);
				}
				
				if($finish){
					$linkSitemap='sitemap.xml';
					$fileXML = simplexml_load_file($linkSitemap);
					$urlTab = $fileXML->addChild("url");
					$urlTab->addChild("loc",url_base.(parent::seo_list_parent(news_menu,NULL,trim($_POST['cb_select']))).$_POST['txt_seo_key'].'.html');
					$urlTab->addChild("changefreq",'always');
					$urlTab->addChild("priority",'0.9');
					
					$fileXML->asXML($linkSitemap);					
					echo '<script type="text/javascript">window.parent.location.reload();</script>';
				}
			
			}
			
			/*Seo*/
			$this->lang['val_seo_key'] ='';
			$this->lang['val_title_page'] ='';
			$this->lang['val_keywords'] = '';
			$this->lang['val_description'] ='';
			$this->lang['val_seo_h1'] ='';
			$this->lang['val_seo_h2'] ='';
			$this->lang['val_seo_p'] ='';
			
			$this->lang['valID'] = isset($_POST['ID'])?$_POST['ID']:NULL;;
			$this->lang['txt_newValue'] = isset($_POST['txt_titlenew'])?$_POST['txt_titlenew']:NULL;
			$this->lang['txt_tagsValue'] = isset($_POST['txt_tags'])?$_POST['txt_tags']:NULL;
			$this->lang['file_pictureValue'] ='';
			$this->lang['txt_detailsValue'] ='';
			$this->lang['acnew'] ='add';
			$this->lang['titlePageEditor'] = $this->lang['titlenewEditor'];
			$this->lang['txt_textarea'] = isset($_POST['txt_textarea'])?$_POST['txt_textarea']:NULL;
			$strHtmlContent = $this->khithe_display('editor');
			$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
			$resultHtml = $this->getCategory(0,'',0,0,1);
			$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function edit_action()
	{
		
		if($_GET['iframe']=='true')
		{
			if(isset($_POST['txt_titlenew']) && $_POST['txt_titlenew']!=""){
				
				if(parent::mdcheckSeoKey($_POST['txt_seo_key'])>0 && $_POST['txt_seo_key'] != $_POST['txt_seo_key_hi'])
				{
					echo '<script type="text/javascript">alert("'.$this->lang['sms_same_key'].'");</script>';
				}
				else{
					$finish = parent::updateInsertnew($_POST);
				}
				
				if($finish){
					echo '<script type="text/javascript">window.parent.location.reload();</script>';
				}
			}
					
			$row = parent::selectnew($this->valUrl(1));
			
			if($_SESSION['Mod']=='Members' && $row['NewOk']=='true')
			{
				echo 'Bạn không có quyền'; exit;
			}
		
			$this->lang['txt_newValue'] = $row['Title'];
			$this->lang['file_pictureValue'] =$row['Picture'];
			$this->lang['valID'] =$row['ID'];
			$this->lang['acnew'] ='edit';
			$this->lang['titlePageEditor'] = $this->lang['titlenewEdit'];
			$this->lang['txt_textarea'] = $row['FullContents'];
			
			/*Seo*/
			$this->lang['val_seo_key'] =$row['SeoKey'];
			$this->lang['val_title_page'] =$row['TitlePage'];
			$this->lang['val_description'] =$row['Description'];
			$this->lang['val_keywords'] =$row['KeyWords'];
			$this->lang['val_seo_h1'] =$row['SeoH1'];
			$this->lang['val_seo_h2'] =$row['SeoH2'];
			$this->lang['val_seo_p'] =$row['SeoP'];
			
			$strHtmlContent = $this->khithe_display('editor');
			$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
			$resultHtml = $this->getCategory(0,'',0,$row['CategoryID'],1);
			$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	
	}
	
	public function index_ajax_action(){
		
		switch ($this->valUrl(2))
		{
			case 'change':
			
				return $this->index_action();
				
			break;
			
			case 'yesok':
			if($_SESSION['Mod']=='Admin')
				{
					$arr = explode(',',$this->valUrl(3));
					foreach ( $arr as $ID ){
						$finish = parent::setCheckedNewOk($ID,'true');
					}
					return $this->index_action();
					
				}else{
					return $this->index_action();
				}
				
			break;
			
			case 'nook':
				if($_SESSION['Mod']=='Admin')
				{
					$arr = explode(',',$this->valUrl(3));
					foreach ( $arr as $ID ){
						$finish = parent::setCheckedNewOk($ID,'false');
					}
					return $this->index_action();
					
				}else{
					return $this->index_action();
				}
				
			break;
			
			case 'open':
				$arr = explode(',',$this->valUrl(3));
				foreach ( $arr as $ID ){
					$finish = parent::setCheckednews($ID,'true');
				}
				if($finish)
				return $this->index_action();
				
			break;
			
			case 'close':
				if($_SESSION['Mod']=='Admin')
				{
					$arr = explode(',',$this->valUrl(3));
					foreach ( $arr as $ID ){
						$finish = parent::setCheckednews($ID,'false');
					}
					if($finish)
					return $this->index_action();
				}
				else{
				
					return $this->index_action();
				}
			break;
			
			case 'del':
				if($_SESSION['Mod']=='Admin')
				{
					$arr = explode(',',$this->valUrl(3));
					foreach ( $arr as $ID ){
						$finish = parent::delCheckednews($ID);
					}
					if($finish)
					return $this->index_action();
					

				}
				else{
				
					return $this->index_action();
				}
			break;
			
		}	
		
	}
	
	private function getCategory($ID, $Order, $indent, $selected, $stop)
	{	
		$indent +=1;
		$resultHtml = '';
		$resultMenu = parent::getMenunews($ID);
		while( $rowMenu = mysql_fetch_assoc($resultMenu) )
		{
			$arrMenu = array(
				"cell_ID"=>$rowMenu['ID'],
				"cell_Value"=>str_repeat("&nbsp;&nbsp;", $indent).$rowMenu['Category'],
				"cell_selected"=>$selected==$rowMenu['ID']?' selected ':'',				
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['category'],$arrMenu);
			if(isset($rowMenu['ID'])&&$stop==1) 
			$resultHtml .=$this->getCategory($rowMenu['ID'], $rowMenu['Order'], $indent, $selected, $stop);
			
		} mysql_free_result($resultMenu);
		
		return $resultHtml;
	}
	
	private function getnew($ID, $limit)
	{	
		$resultHtml = null;
		$indent = NULL;
		$resultnew = parent::getSqlnews( $ID, intval($this->valUrl(2)), intval($limit) );
		while( $rownew = mysql_fetch_assoc($resultnew) )
		{
			$arrnew = array(
				"cell_ID"=>$rownew['ID'],
				"cell_1"=>'<input name="delId[]" type="checkbox" value="'.$rownew['ID'].'" />',
				"cell_2t"=>$rownew['NewOk'],
				"cell_2"=>ucfirst($rownew['Title']),
				"cell_3"=>$this->cutString(strip_tags(str_replace('&nbsp;','',$rownew['FullContents'])), 80),
				"cell_4"=>'<a target="_new" href="'.url_base.'uploads/news/'.$rownew['Picture'].'">Xem</a>',
				"cell_5"=>$rownew['Status']=='true'?$this->lang['enabled']:$this->lang['disabled'],
				"class_cell"=>$rownew['Status']=='true'?"cell_groupitem$indent":"cell_groupitem_ex$indent",
				
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['new'],$arrnew);

		} mysql_free_result($resultnew);
		
		return $resultHtml;
	}


}
?>